<?php
/**
 * Create By: PhpStorm
 * User: yihua
 * File: OssWorker.php
 * Date: 2017/1/11
 * Time: 2:36
 */
namespace app\library\aliyun\oss;

use app\library\CommonConfig;
use OSS\OssClient;
use OSS\Core\OssException;
use app\library\aliyun\sts\StsWorker;
use think\Log;

class OssWorker
{
    //用户上传文件，只能上传到自己的文件夹下面
    public function userPutObject($user_id, $objectName, $content)
    {
        try {
            $ossClient = $this->getOssClient($user_id);
            $realObjectName = $user_id . '/' . $objectName;
            $ossClient->putObject(CommonConfig::OSS_BUCKET, $realObjectName, $content);
            //$ret = $ossClient->getObject($bucket, $object);
            return $this->getObjectUrl($user_id, $objectName);
        } catch (OssException $e) {
            Log::alert($e->getMessage());
            return false;
        }
    }

    public function uploadFile($user_id, $objectName, $filePath)
    {
        try {
            $ossClient = $this->getOssClient($user_id);
            $realObjectName = $user_id . '/' . $objectName;
            //$filePath = '/home/work/123.jpg';
            $ossClient->uploadFile(CommonConfig::OSS_BUCKET, $realObjectName, $filePath);
            return $this->getObjectUrl($user_id, $objectName);
        } catch (OssException $e) {
            Log::alert($e->getMessage());
            return false;
        }

    }

    public function getObject($user_id, $objectName)
    {
        try {
            $ossClient = $this->getOssClient($user_id);
            $objectName = $user_id . '/' . $objectName;
            $ret = $ossClient->getObject(CommonConfig::OSS_BUCKET, $objectName);
            return $ret;
        } catch (OssException $e) {
            Log::alert($e->getMessage());
            return false;
        }

    }

    public function getObjectUrl($user_id, $objectName)
    {
        $url = 'http://' . CommonConfig::OSS_BUCKET . '.' . CommonConfig::OSS_ENDPOINTS . '/' . $user_id . '/' . $objectName;
        return $url;

    }

    public function downloadObject()
    {

    }

    public function listbject()
    {

    }

    public function getOssClient($user_id)
    {
        $endpoint = CommonConfig::OSS_ENDPOINTS;
        $objSts = new StsWorker($user_id);
        $sts = $objSts->getToken();
        if (false === $sts) {
            Log::alert(__METHOD__ . 'STS getToken failed');
            return false;
        }
        $accessKeyId = $sts['Credentials']['AccessKeyId'];
        $accessKeySecret = $sts['Credentials']['AccessKeySecret'];
        $token = $sts['Credentials']['SecurityToken'];
        try {
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false, $token);
            return $ossClient;
        } catch (OssException $e) {
            Log::alert(__METHOD__ . $e->getMessage());
            return false;
        }
    }

    public function test()
    {
        /*
        $user_id = 'wanyihua';
        $endpoint = CommonConfig::OSS_ENDPOINTS;
        $objSts = new StsWorker($user_id);
        $sts = $objSts->getToken();
        if(false === $sts){
            Log::alert(__METHOD__.'STS getToken failed');
            return false;
        }
        $accessKeyId = $sts['Credentials']['AccessKeyId'];
        $accessKeySecret = $sts['Credentials']['AccessKeySecret'];
        $token = $sts['Credentials']['SecurityToken'];
        */
        try {
            //$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint,false,$token);
            $user_id = 'wanyihua';
            $ossClient = $this->getOssClient($user_id);
            $object = $user_id . '/' . 'wan.txt';
            $bucket = CommonConfig::OSS_BUCKET;
            $ossClient->putObject($bucket, $object, 'xxxxxxxxxx');
            $ret = $ossClient->getObject($bucket, $object);
            return true;
            //$ossClient->listBuckets();
        } catch (OssException $e) {
            print $e->getMessage();
            return false;
        }


        /*
        var_dump($sts);
        return gettype($sts);
        $accessKeyId = $sts['Credentials']['AccessKeyId'];
        $accessKeySecret = $sts['Credentials']['AccessKeySecret'];
        $token = $sts['Credentials']['SecurityToken'];


        $accessKeyId = "LTAIYWyvuK3PbEJe";
        $accessKeySecret = "d071ruziquSe683dBExXtG31pz54OW";


        $endpoint = "manshe-private.oss-cn-beijing.aliyuncs.com";
        $bucket = 'manshe-private';
        $object = "a.txt";
        $content = file_get_contents(__FILE__);
        try{
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
            $ossClient->putObject($bucket, $object, $content);
        } catch(OssException $e) {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return;
        }

        try {
            //$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false ,$token);
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
            $ossClient->putObject($bucket, $object, $content);

            $res = $ossClient->getObject($bucket, $object);

            $listObjectInfo = $ossClient->listObjects($bucket);
            $objectList = $listObjectInfo->getObjectList();
            if (!empty($objectList)) {
                foreach ($objectList as $objectInfo) {
                    print($objectInfo->getKey() . "\t" . $objectInfo->getSize() . "\t" . $objectInfo->getLastModified() . "\n");
                }
            }

            $ossClient->deleteObject($bucket, $object);
        } catch (OssException $e) {
            print $e->getMessage();
        }
        */
    }
}
